#include "../Unit--/unit--.h"
#include "../../wmi_filedirectory.h"
#include "../../wmi_iwinobject.h"
#include "../../wmi_connection.h"
#include "../../wmi_connectionmanager.h"
#include "../../wmi_result.h"
#include "../../wmi_constants.h"
#include <comdef.h>
#include <Wbemidl.h>
#include <cstring>
#include <iostream>

# pragma comment(lib, "wbemuuid.lib")

using namespace unit_minus;

testSuite(WMIReaderSuite)

testCase(FileDirectoryTest, WMIReaderSuite)
{
    cout << "---------- Start FileDirectory Test ----------" << endl;
	wstring resD;
	wstring resF;
    vector<wstring> tokens;
    wstring str(C_DIR);
    WMI_Utils::tokenize(str, tokens, DELIMITER);
    vector<wstring>::iterator the_iterator = tokens.begin();
    bool first = true;
    while( the_iterator != tokens.end() )
    {
        if( first)
        {
            first = false;
            resD.append(L"SELECT * FROM Win32_Directory WHERE Path = '");
			resF.append(L"SELECT * FROM CIM_DataFile WHERE Path = '");
        }
        else
        {
            resD.append(DOUBLE_SLASH);
            resD.append(*the_iterator);
			resF.append(DOUBLE_SLASH);
            resF.append(*the_iterator);
        }
        ++the_iterator;
    }

    resD.append(FINAL_DOUBLE_SLASH);    
	resF.append(FINAL_DOUBLE_SLASH);    

	WMI_Connection con;
	WMI_ConnectionManager* mng = new WMI_ConnectionManager();
	WMI_HResult* whres = mng->initCOMSecurityConfiguration();

	mng->createConnection(PC_LOCAL_USER, PC_LOCAL_PASSWORD, PC_NAMESPACE, con);
    delete whres;
	whres = con.executeQuery(resD.c_str());
    IEnumWbemClassObject* pEnumeratorD = NULL;
	pEnumeratorD = con.getIEnumWbemClassObject();
	WMI_Result<WMI_FileDirectory> resultD;
    resultD.setParent(tokens[tokens.size()-1]);
	resultD.fillVector(pEnumeratorD);

    resultD.showObjects();
    system("PAUSE");

    delete whres;
	whres = con.executeQuery(resF.c_str());
    IEnumWbemClassObject* pEnumeratorF = NULL;
	pEnumeratorF = con.getIEnumWbemClassObject();
	WMI_Result<WMI_FileDirectory> resultF;
    resultF.setParent(tokens[tokens.size()-1]);
	resultF.fillVector(pEnumeratorF);

    resultF.showObjects();
    con.close();
    delete whres;
    cout << "---------- End FileDirectory Test ----------" << endl << endl;
    system("PAUSE");
    
}